package com.runen.infinitemovement.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.android.material.appbar.AppBarLayout;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.ColumnActivityNameAdapter;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.fragment.CompanyRankingFragment;
import com.runen.infinitemovement.model.entity.HomeEntity;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.rxnetlibrary.utils.JsonUtils;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CompanyHomeActivity extends BaseActivity {

    @BindView(R.id.btn_finish)
    Button btnFinish;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ll_team_group)
    LinearLayout llTeamGroup;
    @BindView(R.id.ll_title)
    LinearLayout llTitle;
    @BindView(R.id.iv_company_logo)
    ImageView ivCompanyLogo;
    @BindView(R.id.tv_company_name)
    TextView tvCompanyName;
    @BindView(R.id.tv_company_discreble)
    TextView tvCompanyDiscreble;
    @BindView(R.id.rv_title)
    RecyclerView rvTitle;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.coordinator)
    CoordinatorLayout coordinator;
    @BindView(R.id.appBar)
    AppBarLayout appBar;
    //    @BindView(R.id.srl)
//    SwipeRefreshLayout srl;
    private ColumnActivityNameAdapter sportsTypeNameAdapter;
    private String[] sportsTypeNames = new String[]{"运动时长排行", "运动次数排行", "参与活动次数排行"};
    private List<String> typeList = new ArrayList<>();
    private HomeEntity.DataBean.TopCompanyInfoBean topCompanyInfoBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_company_home;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
//        srl.setOnRefreshListener(this);
        initData();
        LinearLayoutManager layoutManager = new GridLayoutManager(this, 3);
        rvTitle.setLayoutManager(layoutManager);
        sportsTypeNameAdapter = new ColumnActivityNameAdapter(this);
        rvTitle.setAdapter(sportsTypeNameAdapter);

        typeList.clear();
        for (int i = 0; i < sportsTypeNames.length; i++) {
            typeList.add(sportsTypeNames[i]);
        }
        sportsTypeNameAdapter.setNewData(typeList);

        sportsTypeNameAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                sportsTypeNameAdapter.setCheckPosition(position);
                sportsTypeNameAdapter.notifyDataSetChanged();
                viewPager.setCurrentItem(position);
            }
        });


        appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (verticalOffset <= -342) {
                    tvTitle.setVisibility(View.VISIBLE);
                } else {
                    tvTitle.setVisibility(View.INVISIBLE);
                }
            }
        });

        initSportsTypeName();
    }

    private void initData() {
        setView();
    }

    private String companyId;

    private void setView() {
        topCompanyInfoBean = JsonUtils.fromJson(getIntent().getStringExtra("CompanyHome"), HomeEntity.DataBean.TopCompanyInfoBean.class);
        if (topCompanyInfoBean != null) {
            GlideUtils.loadImage(this, topCompanyInfoBean.logo, ivCompanyLogo);
            tvCompanyName.setText(topCompanyInfoBean.companyName);
            tvCompanyDiscreble.setText(topCompanyInfoBean.introduction);
            tvTitle.setText(topCompanyInfoBean.companyName);
            companyId = topCompanyInfoBean.companyId;
        }

    }

    private void initSportsTypeName() {
        viewPager.setCurrentItem(0);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                int point = position + 1;
                return new CompanyRankingFragment(point, companyId);
            }

            @Override
            public int getCount() {
                return 3;
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                sportsTypeNameAdapter.setCheckPosition(position);
                sportsTypeNameAdapter.notifyDataSetChanged();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick({R.id.btn_finish, R.id.ll_team_group})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_finish:
                finish();
                break;
            case R.id.ll_team_group:
                Intent intent = new Intent(this, TeamGroupActivity.class);
                intent.putExtra("CompanyInfo", JsonUtils.toJson(topCompanyInfoBean));
                startActivity(intent);
                break;
        }
    }

//    @Override
//    public void onRefresh() {
//        initData();
//        EventBus.getDefault().post(new EventBusEntity(55));
//    }
}
